package com.example.jdbc.redis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

public class JedisUtil {
    @Value("{redis.cache.nodes}")
    private String nodes;
    @Bean
    public JedisCluster getJedisCluster(){
        GenericObjectPoolConfig genericObjectPoolConfig=new GenericObjectPoolConfig();
        String[] serverArray=nodes.split(";");
        Set<HostAndPort> nodeSet = new HashSet<HostAndPort>();
        for (String  server:serverArray) {
            String[] ipAndPort=server.split(":");
            HostAndPort hostAndPort=new HostAndPort(ipAndPort[0],Integer.parseInt(ipAndPort[1]));
            nodeSet.add(hostAndPort);
        }
        return new JedisCluster(nodeSet,genericObjectPoolConfig);
    }

    /**
     * @return the nodes
     */
    public String getNodes(){
        return nodes;
    }
    /**
     * @Param   nodes the nodes to set
     */
    public void setNodes(String nodes){
        this.nodes=nodes;
    }
}
